/**
* @file response_horizontal_flip_image.cpp
*
*	 Nel file response_horizontal_flip_image.cpp viene implementata la classe 
*	 Response_Horizontal_Flip_Image
*	
* @author Favale Angelo
* @author Fagioli Alessio
*
* @version 1.0
*/

#include "response_horizontal_flip_image.h"

Response_Horizontal_Flip_Image::Response_Horizontal_Flip_Image(host peer)
{

	//s= new Storage_Image(peer);
	strcpy(this->name, "Horizontal_Flip_Image");
	strcpy(this->entita.ip,peer.ip);
	this->entita.porta=peer.porta;
	this->entita.s=peer.s;


}


bool Response_Horizontal_Flip_Image::code_request(char * file)
{
	char buffer[BUFFER_SIZE],nome_file[BUFFER_SIZE];
	int len_file;
	int i=0;
	
	len_file=get_len_image(file);

	while(file[i++]!='/');
	strcpy(nome_file,&file[i]);

	sprintf(buffer,"%s;%s;%d#",this->name,nome_file,len_file);
	
	if (send(entita.s,buffer,strlen(buffer),0)==-1)
		{		
		printf("Errore invio\n");
		return false;
		}
	
	return true;

}

bool Response_Horizontal_Flip_Image::decode_request(char* buf, char * file, char * lunghezza_file)
	
{
	
	int i=0,y,c;
	char nome_file[BUFFER_SIZE];

	memset(nome_file,0,BUFFER_SIZE);
	memset(lunghezza_file,0,BUFFER_SIZE);

	
	strcpy(nome_file,"foto/");
	y=strlen(nome_file);//aggiungo ./foto/ per memorizzare il file nella cartella "foto"

	
	while(buf[i]!=';' && i<BUFFER_SIZE)
	{
		nome_file[y]=buf[i];
		i++;
		y++;
	}

	if(i==BUFFER_SIZE)
		return false;
	i++;
	c=i;

	
	while(buf[i]!='#')
	{
		lunghezza_file[i-c]=buf[i];
		i++;
	}
		
	
	if(i==BUFFER_SIZE)
		return false;
	
	memset(file,0,BUFFER_SIZE);	
	strcpy(file,nome_file);

	return true;
 
}


bool Response_Horizontal_Flip_Image::code_response(bool verify,char * nome_file)
{
	int n;
	char msg[BUFFER_SIZE];	
	int send_ok;
	
	if(!verify)
	{	
		send_ok=send(entita.s,"0;",2,0);
		if(send_ok!=-1)			
			return true;
		else
			return false;
	}

	if(send(entita.s,"1;",2,0)==-1)
			return false;
	
	n=get_len_image(nome_file);

	if(n<=0)
		return false;	

	memset (msg,0,BUFFER_SIZE);
		
	sprintf(msg,"%d#",n);

	if (send(entita.s,msg,strlen(msg),0)==-1) //invio lunghezza del file
		{		
		printf("Errore invio\n");
		return false;
		}

	return true;

}


bool Response_Horizontal_Flip_Image::decode_response(char * lunghezza_file )
{

	char buffer[3];
	int i=0;

	memset(buffer,0,2);
	memset(lunghezza_file,0,BUFFER_SIZE);
	
	recv(entita.s,buffer,2,0);

	buffer[2]='\0';
		
	if(buffer[0]=='0')
		return false;
	
	memset(lunghezza_file,0,BUFFER_SIZE);
	
	do
	{
		recv(entita.s,&lunghezza_file[i],1,0);	
		i++;
		
	}while(lunghezza_file[i-1]!='#');

	lunghezza_file[i-1]='\0';

	return true;

}


bool Response_Horizontal_Flip_Image::receive_image (char* nome_file, int len_file)
{
	char buffer[BUFFER_SIZE];
	FILE *f;
	int i,n;

	f=fopen(nome_file,"wb");
	if(f==NULL)
	{			
		printf("Errore apertura file\n");
		return false;
	}
	
	memset(buffer,0,BUFFER_SIZE);
	
	n=0;

	while(n<len_file)
	{
		i=recv(entita.s,buffer,sizeof(buffer),0);
		fwrite(buffer,1,i,f);
		n+=i;		
	}			

	fclose(f);

	return true;
}



bool Response_Horizontal_Flip_Image::send_image (char * file){

	FILE *fp;
	int n,i;
	char msg[BUFFER_SIZE];
	

	memset (msg,0,BUFFER_SIZE);
	i=0;

	if(!(fp=fopen(file,"rb")))
		return false;

	do
	{
	 	n=fread(msg,1,512,fp);
		if(send(entita.s,msg,n,0)==-1)
			{		
			printf("Errore invio\n");
			return false;
			}
		i+=n;
	}while(n>0);

	fclose (fp);

	return true;

}	

